package repository

import (
	"freshfood/model"
	"freshfood/query"
	"freshfood/utils"
	"github.com/jinzhu/gorm"
)

type OrderRespository struct {
	db *gorm.DB
}

type OrderRepoInterface interface {
	List(req *query.ListQuery) (Orders []*model.Order, err error)
}

func (repo *OrderRespository) List(query *query.ListQuery) (orders []*model.Order,err error){

	db := repo.db
	pageSize := query.PageSize
	page := query.Page
	limit, offset := utils.Page(pageSize*page,page)

	err = db.Model(&model.Order{}).Offset(offset).Limit(limit).Find(&orders).Error
	if err != nil {
		return nil,err
	}

	return orders,nil
}
